<template>
	<div class="login">
		<ul class="formarea">
			<li>
				<label class="lit">账号：</label>
				<input type="text" placeholder="手机号码" class="textbox" v-model='login.username' />
			</li>
			<li>
				<label class="lit">密码：</label>
				<input type="password" placeholder="登陆密码" class="textbox" v-model='login.password' />
			</li>
			<li class="liLink lg_liLink">
				<span><label><input type="checkbox" :checked='checked' v-model="checked" />记住密码</label></span>
				<span><a href="register.html">新用户注册</a></span>
				<span><a href="find_pwd.html">忘记密码?</a></span>
			</li>
			<li>
				<input type="button" value="立即登陆" @click='loginSubmit' />
			</li>
		</ul>
	</div>

</template>

<script>
	import store from '@/store/index.js';
	export default {
		data() {
			return {
				login: {
					username: '',
					password: ''
				},
				checked: true
			}
		},
		mounted() {
			var localUsername = localStorage.getItem('username');
			var localPassword = localStorage.getItem('password');
			if (localUsername != 'undefined' || localUsername != '') {
				this.login.username = localUsername;
			}
			if (localPassword != 'undefined' || localPassword != '') {
				this.login.password = localPassword;
			}
		},
		methods: {
			loginSubmit() {
				if (this.login.username == '') {
					mui.toast('用户名不能为空');
					return false;
				} else if (this.login.username != 'admin') {
					mui.toast('用户名输入不正确');
					return false;
				} else if (this.login.password == '') {
					mui.toast('密码不能为空');
					return false;
				} else if (this.login.username == 'admin' && !this.checked) {
					this.userLogin();
					this.unSetLocalUserInfo();
					this.login = {};
				} else if (this.login.username == 'admin' && this.checked) {
					this.userLogin();
					this.setLocalUserInfo();
				}
			},
			unSetLocalUserInfo() {
				localStorage.setItem('username', '');
				localStorage.setItem('password', '');
			},
			setLocalUserInfo() {
				localStorage.setItem('username', this.login.username);
				localStorage.setItem('password', this.login.password);
			},
			userLogin() {
				mui.toast('登录成功');
				localStorage.setItem('admin', 'admin');
				localStorage.setItem('logined', 'true');
				store.dispatch('updateLogined', true);
				this.$router.push('index');
			},
		}

	}
</script>

<style>
</style>
